<?php

namespace App\Admin\Controllers;

use App\Model\Address;
use App\Model\Order;
use App\Model\Rule;
use App\Model\Shop;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Illuminate\Http\Request;

class ShopController extends AdminController
{
    /*
     * 展示商品
     */
    public  function shopShow()
    {
        $allData = Shop::all()->toArray();
        return view('shop.shop-show',compact('allData'));
    }
    /*
     * 商品详情页面
     */
    public  function  shopDetail($id)
    {

        $oneData = Shop::join('rules','rules.id','=','shops.rule_id')->find($id);

        return view('shop.shop-detail',compact('oneData'));

    }
    public  function  orderShow($id)
    {
        $user_id = 1;

        //根据用户id查询出收货信息
        $addressData = Address::where('user_id',$user_id)->get()->toArray();
        //根据用户id查询商品信息
        $shopData = Shop::where('user_id',$user_id)->first()->toArray();
        //根据商品信息查询满减优惠
        $ruleData  = Rule::where('id',$shopData['rule_id'])->get()->toArray();

        //组装要添加的数据
        $row = [
          'user_id'=>$user_id,
          'shop_id' =>$shopData['id'],
          'order_num'=>3,
            'price' => $shopData['price'],
        ];

        $res =  Order::create($row);

       return view('shop.shop-order',compact('addressData','shopData','ruleData','res'));
    }
    /*
     * 展示添加收货信息页面
     */
    public function  addressAdd()
    {
        return view('shop.address-add');
    }
    /*
     * 添加收货地址
     */
    public  function  addressAddDo(Request $request)
    {
        //表单验证
        $request->validate([
            'address_name' =>'required',
            'address' =>'required',
            'phone' =>'required',
        ]);
        //接收数据
        $postData = $request->except(['_token']);

         $res =  Address::create($postData);

         if ($res){
             return redirect(route('admin.orderShow',2));
         }
    }
    /*
     * 地图
     */
    public  function map($address)
    {
        $key = "4350c3d1809ec508b183b5df5f192935";

        $url = "https://restapi.amap.com/v3/geocode/geo?";

        $url = $url . 'address=' . $address . '&key=' . $key;

        $res = $this->geturl($url);

        $location = $res['geocodes'][0]['location'];

        $urlRes = "https://restapi.amap.com/v3/staticmap?" . "location=" . $location . "&zoom=10"."&key=".$key;
        return view('shop.shop-map',compact('urlRes'));
    }
    function geturl($url){
        $headerArray =array("Content-type:application/json;","Accept:application/json");
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch,CURLOPT_HTTPHEADER,$headerArray);
        $output = curl_exec($ch);
        curl_close($ch);
        $output = json_decode($output,true);
        return $output;
    }



}
